/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						       combat_object_registration.cpp

	$Header: /game/combat_object_registration.cpp $

	$NoKeywords: $

 ************************************************************************/

#include "precompiled.h"
#include "precompiled.h"
#include "combat_object_registration.h"

#include <assert.h>
#include <memory.h>
#include "combat_object_type.h"
#include "combat_saveable_object.h"

static t_combat_object_factory_base** get_create_functions()
{
	static t_combat_object_factory_base*	functions[k_combat_object_type_count];
	static bool								initialized = false;

	if (!initialized)
	{
		memset( functions, 0, sizeof( functions ));
		initialized = true;
	}
	return functions;
}

t_combat_object_factory_base const* get_combat_object_factory( t_combat_object_type object_type )
{
	return get_create_functions()[object_type];
}

// -------------------------------------------------------------------
// register a combat spell
// -------------------------------------------------------------------
void t_combat_object_registration_base::register_object( t_combat_object_type object_type, 
													     t_combat_object_factory_base* func )
{
	assert( get_create_functions()[object_type] == 0 );
	get_create_functions()[object_type] = func;
};

// -------------------------------------------------------------------
// create a combat spell
// -------------------------------------------------------------------
t_combat_saveable_object* create_combat_object( t_battlefield& battlefield,
											    t_combat_object_type object_type )
{
	t_combat_object_factory_base* func;

	func = get_create_functions()[object_type];

	if (func == 0)
		return 0;

	t_combat_saveable_object* result = func->create( battlefield );

	assert( result->get_object_type() == object_type );
	return result;
}
